package com.smartkey.utils;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;

import com.smartkey.sharedpreferences.SharePreferencesUtil;

public class KeyManager {

	private static final String TAG = "KeyManagers";
	private static volatile KeyManager keyManager;

	public long lastUpTime = 0;
	public int count = 0;
	Context context;
	public Handler handler = new Handler() {
	};
	public Runnable runnable = new Runnable() {
		public void run() {
			doWay(count);
			System.out.println("clickCount:" + count);
			count = 0;
			lastUpTime = 0;

		}
	};

	private KeyManager(Context context) {
		this.context = context;
	}

	private void doWay(int i) {
		SharePreferencesUtil preferencesUtil = new SharePreferencesUtil(context);
		boolean isDefault = false;
		String packageName = null;
		switch (i) {
		case 1:
			isDefault = preferencesUtil.isDefaultSingle();
			packageName = preferencesUtil.getSingleClickValue();
			break;
		case 2:
			isDefault = preferencesUtil.isDefaultDouble();
			packageName = preferencesUtil.getDoubleClickValue();
			break;
		case 3:
			isDefault = preferencesUtil.isDefaulTrifecta();
			packageName = preferencesUtil.getTrifectaClickValue();
			break;
		case 4:
			break;
		}
		final String pn = packageName;
		final boolean isd = isDefault;
		new Thread(new Runnable() {

			@Override
			public void run() {

				Log.d(TAG, String.format("要打开的app为：%s, 是否默认app：%s", pn, isd));
				Looper.prepare();
				AppUtil.launchAppByPackageName(context, pn, isd);
			}
		}).start();
	}

	public static synchronized KeyManager getInstance(Context context) {

		if (keyManager == null) {
			keyManager = new KeyManager(context);
		}

		return keyManager;
	}

}
